package com.example.service.impl;


import com.example.dao.MemberDao;
import com.example.dao.OrderDao;
import com.example.dao.OrderSettingDao;
import com.example.dao.SetMealDao;
import com.example.domain.message.MessageConstant;
import com.example.domain.pojo.Member;
import com.example.domain.pojo.Order;
import com.example.domain.pojo.OrderSetting;
import com.example.domain.result.Result;
import com.example.domain.utils.DateUtils;


import com.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    //注入dao
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private SetMealDao setMealDao;

    //根据用户微信,验证码添加预约信息
    @Override
    public Result order(Map map) throws Exception {

        //创建返回的order
        Order order=new Order();
        //用户体检预约方式
        String type= (String) map.get("orderType");
        //从map集合中取出套餐id setmealId
        String setmealId = (String) map.get("setmealId");
        //会员id
        Integer memberId=0;


        //1:将日期取出来,判断是否可以进行预约设置 orderDate='2019-10-29 00:00:00'
        Date date= DateUtils.parseString2Date((String) map.get("orderDate"));;//map集合传递到后端是字符串形式

        //根据日期查询是否可以进行预约设置
         OrderSetting orderSetting = orderSettingDao.selectDate(date);
        //判断预约套餐是否为空
        if(orderSetting==null){
            //不可预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2:套餐日期不为空,判断当前人数是否可以进行预约
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(number-reservations<=0){
            //已经预约满,不可预约
            return new Result(false,MessageConstant.ORDER_FULL);
        }


        //3:判断是否是会员,会员直接尽心预约,不是,需要先进行注册
        //3.1:从map集合获取电话号码
        String  telephone = (String) map.get("telephone");

        //3.2:根据电话查询会员表数据,查看会员是否存在
        Member member= memberDao.findmember(telephone);



        if(member!=null){ //用户是会员
            //判断用户是否重复预约,查询order表 ,是否同一天同一个套餐检查 套餐ID存在,是用户如何判断??
             //会员id
             memberId = member.getId();
            //三个条件同时成立才可以确定用户已经预约
            order.setMemberId(memberId);
            order.setOrderDate(date);
            order.setSetmealId(Integer.parseInt(setmealId));
            //查询是否存在重复预约
            List<Order> result_order =orderDao.findOrder(order);

           if(result_order!=null&&result_order.size()>0){
               //用户已经预约
               return new Result(false,MessageConstant.HAS_ORDERED);
           }

        }else{
            //创建会员,添加 到会员列表
            member=new Member();
            String name = (String) map.get("name");//体检人的名字
            String  sex = (String) map.get("sex");
            String idCard= (String) map.get("idCard");//身份证号

            member.setName(name);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setSex(sex);
            member.setRegTime(new Date());//注册会员的时间
            //此用户不是会员,在member表中进行注册
            memberDao.addmember(member);
            //新会员的id
            memberId= member.getId();
        }

        //套餐id+1
        orderSetting.setReservations(reservations+1);
        //设置可预约人数
        orderSettingDao.editreservationsByDate(orderSetting);
        //根据提交的信息 日期和套餐id添加到订单表中
        order.setMemberId(memberId);
        order.setOrderDate(date);
        System.out.println(Integer.parseInt(setmealId));
        order.setSetmealId(Integer.parseInt(setmealId));
        order.setOrderType(type);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //添加订单表中
        orderDao.add(order);
        //体检预约成功
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //根据order_id查询order返回前端预约成功页面
    @Override
    public Map findOrderById(Integer id) throws Exception { //直接查询三张表
        Map  map1 = orderDao.selectOrderById(id);
        Date orderDate = (Date) map1.get("orderDate");
        String s = DateUtils.parseDate2String(orderDate);
        map1.put("orderDate",s);
        return map1;
    }

    //根据id查询订单数
    @Override
    public Long findCount(Integer id) {
      Long count= orderDao.findCountById(id);
      return count;
    }

    //根据日期查询今日预约数
    @Override
    public Long findDateCount(String date) {
        Long count=orderDao.findDateCount(date);
        return count;
    }

    //查询今日到诊数
    @Override
    public Long findvisitdate(String date) {
        //字符串比对
        Map<String,String> map=new HashMap<>();
        map.put("date",date);
        map.put("status","到诊");
        Long count=orderDao.findVisitCount(map);
        return count;
    }

    //根据本周时间查询本周预约数
    @Override
    public Long findWeekCount(Map<String, String> week) {
        Long count=orderDao.findWeekCount(week);
        return count;
    }

    //根据本周时间查询本周到诊数
    @Override
    public Long findvisitWeek(Map<String, String> week) {
        week.put("status","到诊");
        Long count=orderDao.findVisitWeekCount(week);
        return count;
    }
}
